package com.wyc.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Component
public class JWTUtils {
    //生成jwt --固定算法秘钥为hello
    public String build(String id){
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        String jwt = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, "hello")
                .setClaims(map)
                .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60*60*24*7))//一周后过期
                .compact();
        return jwt;
    }

    //解析jwt

    public Claims parse(String jwt){
        Claims claims = Jwts.parser()
                .setSigningKey("hello")
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }
}
